package com.fdb.efp.esb.service.impl.psd;

import com.alibaba.fastjson.JSONObject;
import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.constant.ESBClientConstance;
import com.fdb.efp.esb.common.constant.EsbBizEnums;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.basic.framework.core.exception.ESBException;
import com.fdb.efp.esb.service.bo.req.psd.PsdSendMsgToTaxReqBean;
import com.fdb.efp.esb.service.bo.resp.psd.PsdSendMsgToTaxRespBean;
import com.fdb.efp.esb.service.client.ESBRequestBuilder;
import com.fdb.efp.esb.service.client.EsbBaseBean;
import com.fdb.efp.esb.service.facade.psd.PsdSendMsgToTaxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("psdWdSendMsgToTaxService")
public class PsdSendMsgToTaxServiceImpl implements PsdSendMsgToTaxService {
    private static Logger logger = LoggerFactory.getLogger(PsdSendMsgToTaxServiceImpl.class);

    @Override
    public PsdSendMsgToTaxRespBean applyPsdSendMsgToTax(PsdSendMsgToTaxReqBean psdSendMsgToTaxReqBean) throws ESBException {
        logger.info("======>调用税务局信贷审批结果通知接口【" + psdSendMsgToTaxReqBean.getApplySeq()+ "_30220006_04】开始<======");
        PsdSendMsgToTaxRespBean psdSendMsgToTaxRespBean = null;
        try {
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
                    withINSTID(ESBClientConstance.bdInstId).withGLBLSRLNO(psdSendMsgToTaxReqBean.getApplySeq()).build();
            EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(psdSendMsgToTaxReqBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30220006)
                    .withScene(ESBConstant.SCENE_NO_04)
                    .create()
                    .sendAndReceive();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                psdSendMsgToTaxRespBean = recvBean.getBody(PsdSendMsgToTaxRespBean.class);
            } else {
                throw new ESBException(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
            }
            logger.info("调用税务局信贷审批结果通知接口【30220006_04】结束，返回信息：" + JSONObject.toJSONString(psdSendMsgToTaxRespBean));
        } catch (Exception e) {
            logger.error("调用税务局信贷审批结果通知接口【30220006_04】出现异常：" + e.getMessage());
            throw new ESBException("调用税务局信贷审批结果通知接口【30220006_04】出现异常：" + e.getMessage());
        } finally {
            logger.info("======>调用税务局信贷审批结果通知接口【" + psdSendMsgToTaxReqBean.getApplySeq() + "_30220006_04】结束<======");
        }
        return psdSendMsgToTaxRespBean;
    }
}